//index.js
//获取应用实例
var app = getApp()
Page({
	data: {
		root:'',
		siteroot:app.setting.siteroot,
		att_url:app.setting.att_url,
		account:app.setting.account,
		pagetitle:'工作台',
		
	},
	onReady: function (){
	},
	onPullDownRefresh: function (){
		var the=this;
		var opt={};
		the.onLoad(opt);
		setTimeout(function(){
			wx.stopPullDownRefresh()
		},1000);
	},
	onLoad: function (opt){
		var page=this;
		wx.setNavigationBarTitle({'title':page.data.pagetitle});
		var route=page.route;
		page.setData({
			route:route,
		});
		if(route){
			var path=route.slice(0,-6).split('/');
			app.access.plugin=path.shift();
			app.access.action=path.join('.');
		}
		if(opt){
			app.access.query =app.util.made_query(opt,true);
			app.access.op=opt.op?opt.op:'';
		}
		app.util.get(app.access,function(pagedata){
			//console.log('pagedata');console.log(pagedata);
			page.setData({
				pagedata:pagedata
			});
			page.get_tmpids();
		});
	},
	get_tmpids: function (){
		var the=this
		app.util.get('jy_weishop/get_data/get_tmpids/',function(res){
			//console.log('get_tmpids');console.log(res);
			the.setData({
				'pagedata.tmplIds':res.list,
			});
			if(!app.isShowSubMsg && res.list && res.list[0]){
				app.util.getsubmsgSet(res.list,function(ShowSubMsg){
					if(ShowSubMsg){
						app.isShowSubMsg=true;
						the.setData({
							ShowSubMsg:true,
						});
					}
				});
			}
		});
	},
	//订阅消息
	SubscribeMessage: function () {
		var the=this
		var pagedata=the.data.pagedata
		the.setData({
				ShowSubMsg:false,
		});
		wx.requestSubscribeMessage({
			'tmplIds':pagedata.tmplIds,
			success:function (res) {
				console.log('SubscribeMessage success');console.log(res);
				if(res[pagedata.tmplIds[0]]=='accept'){
					
				}
			},
			fail:function (res) {
				console.log('SubscribeMessage fail');console.log(res);
			},
		})
	},
	show_verify: function (e) {
		var pagedata=this.data.pagedata
		pagedata.showyewu=pagedata.showyewu?false:true
		this.setData({
			'pagedata.showyewu':pagedata.showyewu
		});
	},
	scanCode:function(){
		wx.scanCode({
		  success (res) {
			 wx.navigateTo({'url':res.path});
		  }
		})
	},
	//保存海报
	saveImageToPhoto: function() {
		var that = this;
		var qrcode=that.data.att_url+that.data.pagedata.store.qrcode
		console.log(qrcode)
		wx.getImageInfo({
			src: qrcode,
			fail(res){
				console.log('保存失败');console.log(res)
			},
			success(res){
					if(qrcode){
						wx.saveImageToPhotosAlbum({
							filePath: res.path,
							//filePath:'/resource/images/share-bg01.png',
							success: function() {
								wx.showModal({
									title: '保存图片成功',
									content: '图片已经保存到相册，快去分享吧！',
									showCancel: false,
									success: function(res) {
										that.setData({
											showsetqrcode: false,
											canvasShow: false,
										})
									},
									fail: function(res) {},
									complete: function(res) {},
								});
							},
							fail: function(res) {
								//console.log(res);
								if (res.errMsg ==
									"saveImageToPhotosAlbum:fail cancel") {
									wx.showModal({
										title: '保存图片失败',
										content: '您已取消保存图片到相册！',
										showCancel: false
									});
								} else {
									wx.showModal({
										title: '提示',
										content: '保存图片失败，您可以点击确定设置获取相册权限后再尝试保存！',
										complete: function(res) {
											//console.log(res);
											if (res.confirm) {
												wx.openSetting({}) //打开小程序设置页面，可以设置权限
											} else {
												wx.showModal({
													title: '保存图片失败',
													content: '您已取消保存图片到相册！',
													showCancel: false
												});
											}
										}
									});
								}
							}
						})
					}
			}
		});
	},
  	click:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util[data.func](data.params);
	},
	formSubmit:function(event){
		var data=event.detail.value;
		app.util.post('post',data);
	}
});